<?php
// +----------------------------------------------------------------------
// | Name: 咪乐多管理系统 [ 为了快速搭建软件应用而生的，希望能够帮助到大家提高开发效率。 ]
// +----------------------------------------------------------------------
// | Copyright: (c) 2020~2022 https://www.lvacms.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed: 这是一个自由软件，允许对程序代码进行修改，但希望您留下原有的注释。
// +----------------------------------------------------------------------
// | Author: 西安咪乐多软件 <997786358@qq.com>
// +----------------------------------------------------------------------
// | Version: V1
// +----------------------------------------------------------------------

/**
 * @Name
 * @Description
 * @Auther 西安咪乐多软件
 * @Date 2022/1/26 17:25
 */

namespace Modules\BlogAdmin\Services\Project;


use Modules\Admin\Models\AdminImage;
use Modules\BlogAdmin\Models\AdminProject;
use Modules\BlogAdmin\Services\BaseApiServices;
use Modules\Common\Exceptions\MessageData;

class ProjectServices extends BaseApiServices
{
    public function index(){
        $id = $this->getProjectId();
        $data = AdminProject::query()->with([
            'logoImage'=>function($query){
                $query->select('id','url','open');
            },
            'icoImage'=>function($query){
                $query->select('id','url','open');
            }
        ])->select( "name", "logo_id", "ico_id", "url", "description", "keywords", "status","ext")->find($id)?:$this->apiError(MessageData::GET_API_ERROR);
        $data = $data->toArray();
        $data['gzh_image_to'] = null;
        $data['wx_image_to'] = null;
        $data['about'] = "";
        $data['statement'] = "";
        $data['gzh_image_id'] = null;
        $data['wx_image_id'] = null;

        if($data['ext']){
            $ext = self::unSerialize($data['ext']);
            if(isset($ext['gzh_image_id']) && $ext['gzh_image_id']>0){
                $data['gzh_image_to'] = AdminImage::query()->select('id','url','open')->find($ext['gzh_image_id'])?:null;
            }
            $data['gzh_image_id'] = isset($ext['gzh_image_id'])?$ext['gzh_image_id']:null;
            if(isset($ext['wx_image_id']) && $ext['gzh_image_id']>0){
                $data['wx_image_to'] = AdminImage::query()->select('id','url','open')->find($ext['wx_image_id'])?:null;
            }
            $data['wx_image_id'] = isset($ext['wx_image_id'])?$ext['wx_image_id']:null;
            if(isset($ext['about'])){
                $data['about'] = $this->getReplacePicUrl($ext['about']);
            }
            $data['statement'] = isset($ext['statement'])?$ext['statement']:"";
            $data['title'] = isset($ext['title'])?$ext['title']:"";
            $data['icp'] = isset($ext['icp'])?$ext['icp']:"";
            $data['header'] = isset($ext['header'])?$ext['header']:"";
            $data['footer'] = isset($ext['footer'])?$ext['footer']:"";
            $data['general_embody'] = isset($ext['general_embody'])?$ext['general_embody']:"";
            $data['quick_embody'] = isset($ext['quick_embody'])?$ext['quick_embody']:"";
            $data['robots'] = isset($ext['robots'])?$ext['robots']:"";
        }
        return $this->apiSuccess(MessageData::GET_API_SUCCESS,$data);
    }
    public function update(array $data){
        $id = $this->getProjectId();
        if(isset($data['about']) && $data['about']){
            $data['about'] = $this->setPicUrl($data['about']);
        }
        $data['ext'] = serialize([
            "gzh_image_id" => isset($data["gzh_image_id"])?$data["gzh_image_id"]:null,
            "wx_image_id" => isset($data["wx_image_id"])?$data["wx_image_id"]:null,
            "about"=>isset($data["about"])?$data["about"]:null,
            "statement"=>isset($data["statement"])?$data["statement"]:null,
            "title"=>isset($data["icp"])?$data["title"]:null,
            "icp"=>isset($data["icp"])?$data["icp"]:null,
            "header"=>isset($data["header"])?$data["header"]:null,
            "footer"=>isset($data["footer"])?$data["footer"]:null,
            "general_embody"=>isset($data["general_embody"])?$data["general_embody"]:null,
            "quick_embody"=>isset($data["quick_embody"])?$data["quick_embody"]:null,
            "robots"=>isset($data["robots"])?$data["robots"]:null,
        ]);
        unset($data["gzh_image_id"],$data["wx_image_id"],$data["about"],$data["statement"],$data["icp"],$data["header"],$data["footer"],$data["footer"],$data["general_embody"],$data["quick_embody"],$data["robots"],$data["title"]);
        return $this->commonUpdate(AdminProject::query(),$id,$data);
    }
}
